package com.dnjn.happiness.system.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.system.api.domain.SysUser;
import com.dnjn.happiness.system.domain.PositionLadder;
import com.dnjn.happiness.system.api.domain.SysTeam;
import com.dnjn.happiness.system.domain.vo.ExportTeamVO;
import com.dnjn.happiness.system.domain.vo.TeamLineChartVO;
import com.dnjn.happiness.system.domain.vo.TeamUserSituationVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员架构Mapper接口
 * 
 * @author amber
 * @date 2024-01-02
 */
public interface SysTeamMapper extends  BaseMapper<SysTeam>
{

    SysTeam getTeamByinviteCode(String inviteCode);

    SysTeam getTeamByUserId(Long userId);

    @Update(" UPDATE sys_team SET parent_id = NULL,super_id = NULL WHERE user_id = #{userId} ")
    int unBindParentTeamByUserId(Long userId);

    @Update(" UPDATE sys_team SET parent_id = NULL,super_id = NULL WHERE parent_id = #{teamId} ")
    void unBindChildTeamByTeamId(Long teamId);

    SysTeam queryInfoByTeamId(Long teamId);

    List<SysTeam> getTeamListByUserIdAndParentId(@Param("teamId") Long teamId,@Param("nickName") String nickName);

    @Update(" UPDATE sys_team SET super_id = NULL WHERE super_id = #{teamId} ")
    void unBindSuperTeamByTeamId(Long teamId);

    @Update(" UPDATE sys_team SET super_id = NULL WHERE team_id = #{teamId} ")
    void updateSuperId(Long teamId);

    List<PositionLadder> queryPositionListByTeamId(Long teamId);

    List<SysUser> getUsersByPositionAndTeamId(@Param("map") Map<String, Object> map);

    Page<ExportTeamVO> getPage(@Param("vo") ExportTeamVO vo, Page page);

    List<ExportTeamVO> getPageList(@Param("vo") ExportTeamVO vo);

    List<SysTeam> selectTeamAll(@Param("nickName") String nickName, @Param("deptId") Long deptId);

    List<Long> recursionTeam(Long teamId);


    Integer queryRequiredCommunicateCount(@Param("startInterval") Integer startInterval, @Param("endInterval") Integer endInterval,
                                          @Param("criticalValue") Integer criticalValue, @Param("longList") List<Long> longList);

    Integer queryNormalCount(@Param("startInterval") Integer startInterval, @Param("endInterval") Integer endInterval,
                             @Param("criticalValue") Integer criticalValue, @Param("longList") List<Long> longList);


    Page<TeamUserSituationVO> getGraphicReportsPage(@Param("page") Page page, @Param("map") HashMap<String, Object> map);

    @Select(" SELECT score_avg FROM emo_avg_score_daily WHERE user_id = #{userId} AND DATEDIFF(curr_date,NOW()) = -1 ")
    BigDecimal selectYesterdayScoreAvgByUserId(Long userId);

    @Select(" SELECT DATEDIFF(create_time,NOW()),create_time FROM biz_communicate  WHERE communicate_status = '0' and deleted = '0' and user_id = #{userId} ")
    Integer selectDaysByUserId(Long userId);

    List<TeamLineChartVO> selectDataByWeek(@Param("weekOfYear") String weekOfYear, @Param("userId") Long userId);

    @Select("  SELECT DISTINCT WEEK(curr_date,1) week,YEARWEEK(curr_date,1) weekOfYear FROM emo_avg_score_daily WHERE YEAR(curr_date) = YEAR(NOW()) AND deleted = '0' " +
            "GROUP BY curr_date ORDER BY week ")
    List<TeamLineChartVO> getWeekList();

    SysTeam getInfo(Long userId);

    List<SysTeam> getTeamListByDeptId(Long deptId);

    @Select(" SELECT t1.team_id,t1.user_id,u.nick_name nickName FROM sys_team t LEFT JOIN sys_team t1 ON t.parent_id = t1.team_id LEFT JOIN sys_user u ON t1.user_id = u.user_id " +
            "WHERE t.deleted = '0' AND t1.deleted = '0' AND u.del_flag = '0' AND t.user_id = #{userId} ")
    SysTeam getParentInfoByUserId(Long userId);

    @Select(" SELECT t.parent_id,t.user_id,u.nick_name FROM sys_team t LEFT JOIN sys_user u USING(user_id) WHERE parent_id = #{teamId} AND deleted = '0' AND del_flag = '0' ")
    List<SysTeam> selectChildList(Long teamId);


    void updateAppraiseTime(@Param("userId") Long userId, @Param("format") String format);
}
